package cn.iocoder.yudao.module.product.service.stockinfo;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.product.controller.admin.stockinfo.vo.StockInfoPageReqVO;
import cn.iocoder.yudao.module.product.controller.admin.stockinfo.vo.StockInfoSaveReqVO;
import cn.iocoder.yudao.module.product.dal.dataobject.stockinfo.StockInfoDO;
import cn.iocoder.yudao.module.product.dal.mysql.stockinfo.StockInfoMapper;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;

import java.util.ArrayList;
import java.util.List;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.product.enums.ErrorCodeConstants.STOCK_INFO_NOT_EXISTS;

/**
 * 库存信息 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class StockInfoServiceImpl implements StockInfoService {

    @Resource
    private StockInfoMapper stockInfoMapper;

    @Override
    public Long createStockInfo(StockInfoSaveReqVO createReqVO) {
        // 插入
        StockInfoDO stockInfo = BeanUtils.toBean(createReqVO, StockInfoDO.class);
        stockInfoMapper.insert(stockInfo);
        // 返回
        return stockInfo.getId();
    }

    @Override
    public void createStockInfoBath(List<StockInfoSaveReqVO> createReqVO) {
        // 插入
        List<StockInfoDO> stockInfo = BeanUtils.toBean(createReqVO, StockInfoDO.class);
        stockInfoMapper.insertBatch(stockInfo);
    }

    @Override
    public void updateStockInfo(StockInfoSaveReqVO updateReqVO) {
        // 校验存在
        validateStockInfoExists(updateReqVO.getId());
        // 更新
        StockInfoDO updateObj = BeanUtils.toBean(updateReqVO, StockInfoDO.class);
        stockInfoMapper.updateById(updateObj);
    }

    @Override
    public void deleteStockInfo(Long id) {
        // 校验存在
        validateStockInfoExists(id);
        // 删除
        stockInfoMapper.deleteById(id);
    }

    private void validateStockInfoExists(Long id) {
        if (stockInfoMapper.selectById(id) == null) {
            throw exception(STOCK_INFO_NOT_EXISTS);
        }
    }

    @Override
    public StockInfoDO getStockInfo(Long id) {
        return stockInfoMapper.selectById(id);
    }



    @Override
    public PageResult<StockInfoDO> getStockInfoPage(StockInfoPageReqVO pageReqVO) {
        return stockInfoMapper.selectPage(pageReqVO);
    }

    @Override
    public PageResult<StockInfoSaveReqVO> getStockInfoList(StockInfoSaveReqVO respVO) {
        List<StockInfoSaveReqVO> list = new ArrayList<>();
        // 商品
        respVO.setProductType(0);
        List<StockInfoSaveReqVO> respVO1 = stockInfoMapper.selectBySkuProductType(respVO);
        if (CollectionUtils.isNotEmpty(respVO1)){
            StockInfoSaveReqVO vo = new StockInfoSaveReqVO();
            vo.setName(respVO1.get(0).getCatName());
            vo.setItemList(respVO1);
            vo.setProductType(0);
            list.add(vo);
        } else {
            list.add(new StockInfoSaveReqVO().setProductType(0).setItemList(new ArrayList<>()));
        }

        //包装物
        respVO.setProductType(1);
        List<StockInfoSaveReqVO> respVO2 = stockInfoMapper.selectBySkuProductType(respVO);
        if (CollectionUtils.isNotEmpty(respVO2)) {
            respVO2.forEach(x -> {
                x.setProductCode(x.getSpuName());
            });
            StockInfoSaveReqVO vo = new StockInfoSaveReqVO();
            vo.setName(respVO2.get(0).getCatName());
            vo.setItemList(respVO2);
            vo.setProductType(1);
            list.add(vo);
        } else {
            list.add(new StockInfoSaveReqVO().setProductType(1).setItemList(new ArrayList<>()));
        }
        PageResult<StockInfoSaveReqVO> result = new PageResult<>(list, (long) list.size());
        return result;
    }

    @Override
    public List<StockInfoSaveReqVO> getStockInfoBySpuId(Long spuId) {
        LambdaQueryWrapperX<StockInfoDO> wrapperX = new LambdaQueryWrapperX<>();
        wrapperX.eqIfPresent(StockInfoDO::getSpuId, spuId)
                .eq(StockInfoDO::getOwnerType, 1 );
        return BeanUtils.toBean(stockInfoMapper.selectList(wrapperX), StockInfoSaveReqVO.class);
    }

    @Override
    public void deleteStockInfoBathBySpuId(List<Long> collect2, Long spuId) {
        LambdaQueryWrapperX<StockInfoDO> wrapperX = new LambdaQueryWrapperX<>();
        wrapperX.eqIfPresent(StockInfoDO::getSpuId, spuId)
                .inIfPresent(StockInfoDO :: getOwnerId, collect2)
                .eqIfPresent(StockInfoDO::getOwnerType, 1);
        stockInfoMapper.delete(wrapperX);
    }

}
